package com.jinlejia.bbs.logic.other;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.bbs.bean.GambitBean;
import com.jinlejia.bbs.bean.GambitListBean;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.InformationBean;
import com.jinlejia.bbs.bean.base.SoundBean;
import com.jinlejia.bbs.bean.base.TopicBean;
import com.jinlejia.bbs.http.entity.GambitEntity;
import com.jinlejia.bbs.http.entity.UserEntity;
import com.jinlejia.bbs.http.entity.base.PageEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;
import com.jinlejia.bbs.http.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;

/**
 * @author 王进 on 2019/3/31.
 */

public class OtherQuestionLogic {
    private Context context;
    private GambitListBean bean;
    private LogicCallBack callBack;
    public OtherQuestionLogic(Context context, GambitListBean bean, LogicCallBack callBack){
        this.context = context;
        this.bean = bean;
        this.callBack = callBack;
    }
    private int pageIndex;
    public void refresh(String pin) {
        pageIndex = 1;
        loadData( pageIndex,pin);
    }

    public void load(String pin) {
        pageIndex = pageIndex + 1;
        loadData(pageIndex, pin);
    }

    private void loadData(final int pageIndex, String pin) {
        JSONObject json = new JSONObject();
        json.put("pageIndex", pageIndex);
        json.put("pageSize", 10);
        if (!TextUtils.isEmpty(pin)) {
            json.put("pin", pin);
        }
        httpCallForm(BBSConfig.listMyTopic, json).execute(httpCallBack(context, new HttpCallBack() {
            @Override
            public void onError(Exception e) {
                callBack.onResult(false);
            }

            @Override
            public void onNoResponse(int code) {
                callBack.onResult(false);
            }

            @Override
            public void onResponse(HttpUtils.JsonParse parse) {
                TypeReference<PageEntity<GambitEntity>> reference = new TypeReference<PageEntity<GambitEntity>>() {
                };
                PageEntity<GambitEntity> response = parse.parsePageEntity(reference);
                List<GambitEntity> data = response.getData();
                ArrayList<GambitBean> list = new ArrayList<>();
                for (GambitEntity entity : data) {
                    {
                        GambitBean bean = new GambitBean();
                        //组装 话题
                        TopicBean topic = new TopicBean();
                        String topicTitle = entity.getTopicTitle();
                        topic.setTopicTitle(topicTitle);
                        topic.setTopicCode(entity.getTopicCode());
                        topic.setReviewCount(entity.getReviewNum());
                        bean.setTopic(topic);


                        InformationBean info = null;
                        if (!TextUtils.isEmpty(entity.getContent()) || !TextUtils.isEmpty(entity.getContent())){
                            info = new InformationBean();
                            if (entity.getContentType()==1){
                                info.setMessageContent(entity.getContent()); //回答内容
                            }else if (entity.getContentType()==2){
                                info.setSoundContent(SoundBean.create(entity.getContent())); //回答内容
                            }
                            info.setReviewCount(entity.getReviewNum()); //回答阅读次数
                            info.setLikeCount(entity.getSupportCount()); //回答点赞次数
                            info.setOwnLike(entity.getIsUser() == 1); //是否自己已经点赞


                            //region 组装回答用户
                            BBSUserBean user = new BBSUserBean();
                            user.setUserPin(entity.getUserPin()); //回答用户PIN
                            user.setUserName(entity.getUserName());//回答用户姓名
                            user.setUserAvatar(BBSConfig.headerUrl(entity.getUserImageUrl())); //回答用户头像
                            if (!TextUtils.isEmpty(entity.getAnswerStoreName())){
                                user.setUserAddress(entity.getAnswerStoreName()); //回答用户门店
                            }else {
                                user.setUserAddress(entity.getUserTitleLevel());
                            }
                            user.setUserDuty(entity.getUserTitleLevel()); //回答用户职务
                            info.setUser(user);
                            //endregion
                            List<UserEntity> recordsUserResults = entity.getAttitudesRecordsUserResults();
                            List<BBSUserBean> likeList = new ArrayList<>();
                            if (recordsUserResults!=null){
                                for (UserEntity userEntity:recordsUserResults) {
                                    String imageUrl = userEntity.getImageUrl();
                                    String pin = userEntity.getPin();
                                    BBSUserBean likeUser = new BBSUserBean();
                                    likeUser.setUserAvatar(BBSConfig.headerUrl(imageUrl));
                                    likeUser.setUserPin(pin);
                                    likeList.add(likeUser);
                                }
                            }
                            info.setAnswerCode(entity.getAnswerCode());
                            info.setLikeUserList(likeList);
                        }
                        bean.setInfo(info);

                        list.add(bean);
                    }
                }
                if (pageIndex==1){
                    bean.setGambitBeans(list);
                }else {
                    bean.getGambitBeans().addAll(list);
                }
                boolean pageAvailable = response.getNextPageAvailable();
                callBack.onResult(pageAvailable);
            }
        }));
    }

    public interface LogicCallBack{
        void onResult(boolean hasNextPage);
    }
}
